package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.RoleService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@Controller
@RequestMapping("/employee")
public class EmployeeController {

    private IEmployeeService employeeService;
    private RoleService roleService;
    private IDepartmentService departmentService;

    @Autowired
    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }

    @Autowired
    public void setDepartmentService(IDepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    @Autowired
    public void setEmployeeService(IEmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    @RequestMapping("/list")
    @RequiredPermission(name = "员工列表",expermission = "employee:list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo){
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        List<Department> departments = departmentService.listAll();
         model.addAttribute("departments",departments);
        return "employee/list";
    }

    @RequestMapping("/delete")
    @RequiredPermission(name = "员工删除",expermission = "employee:delete")
    public String delete(Long id){
        if (id!=null){
            employeeService.delete(id);
        }
          return "redirect:/employee/list";
    }

    @RequestMapping("/input")
    @RequiredPermission(name = "员工新增",expermission = "employee:input")
    public String input(Long id, Model model){
        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments",departments);
        List<Role> roles = roleService.selectAll();
        model.addAttribute("roles",roles);
        if (id!=null){
            Employee employee = employeeService.get(id);
             model.addAttribute("employee",employee);
             List<Role>list=roleService.selectByEmpId(id);
                model.addAttribute("list",list);
        }
        return "employee/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name = "员工新增或编辑",expermission = "employee:saveOrUpdate")
    public String saveOrUpdate(Employee employee){
        if (employee.getId()==null){
            employeeService.save(employee);
        }else {
            employeeService.update(employee);
        }
         return "redirect:/employee/list";

    }
    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        response.setHeader("Content-Disposition","attachment;filename=employee.xls");
        Workbook workbook = employeeService.exportXls();
        workbook.write(response.getOutputStream());
    }

    @RequestMapping("/importXls")
    public String importXls(MultipartFile file)throws Exception{
        file.getOriginalFilename();
        employeeService.importXls(file);
        return "redirect:/employee/list";
    }
}
